package com.gljx.web.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gljx.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gljx.web.Entity.TrainType;
import com.gljx.web.Entity.User;
import com.gljx.web.service.CustomLogService;
import com.gljx.web.service.TrainTypeService;

@Controller
@RequestMapping("/")
public class TrainTypeController {
  
	//自动注入
	@Autowired
	private TrainTypeService trainTypeService;
	
	//自动注入
	@Autowired
	private CustomLogService customLogService;
	
	/**
	 * 根据类型名称查询培训类型
	 * @param name
	 * @return
	 * @createTime 2017-11-2 5:20 pm
	 * @author zyq.
	 */
	@ResponseBody
	@RequestMapping(value="/selTrainTypeByName")
	public List<TrainType> selTrainTypeByName(String name){
		List<TrainType> trainTypes=trainTypeService.selTrainTypeByName(name);
		return trainTypes;
	}
	
	/**
	 * 根据公司id查询
	 * @param request
	 * @return
	 */
	@RequestMapping("/queryTrainType/look.action")
    public String queryTrainType(HttpServletRequest request, String flag) {
        //获取操作人信息
	    User user = (User)request.getSession().getAttribute("user");
	    //根据companyId查询
	    List<TrainType> trainTypes = trainTypeService.queryTrainTypeByCompanyId(user.getFcompanyid());
	    //把数据传到页面
	    request.setAttribute("trainTypes", trainTypes);
        request.setAttribute("flag", flag);
        return "/backHome/studentData/trainType/trainTypeList";
	}
	
	/**
	 * 根据公司id和名称查询
	 * @param request
	 * @param fnameL2 查询的名称
	 * @return
	 */
	@RequestMapping("/queryTrainTypeByFnameL2")
	public String queryTrainTypeByFnameL2(HttpServletRequest request,String fnameL2){
		//获取操作人信息
	    User user = (User)request.getSession().getAttribute("user");
		if("".equals(fnameL2)){
            queryTrainType(request, null);//为空就调用查询的方法
        }else{
            //去除空格
            fnameL2 = fnameL2.trim();
            //查询数据
			List<TrainType>trainTypes =	trainTypeService.queryTrainTypeByCompanyIdAndFnameL2(user.getFcompanyid(), fnameL2);
			//把数据传到页面
			request.setAttribute("trainTypes", trainTypes);
			request.setAttribute("fnameL2", fnameL2);
		}
		return "/backHome/studentData/trainType/trainTypeList";
	}
	
	/**
	 * 跳转到添加页面
	 * @param request
	 * @return
	 */
	@RequestMapping("/insertTrainType")
	public String insertTrainType(HttpServletRequest request,String type){
		//获取操作人信息
	    User user = (User)request.getSession().getAttribute("user");
		//获取上一个编码
        /*String number = trainTypeService.selectFnumber(user.getFcompanyid());*/
		//获取编码
        String fnumber = GetFnumber.fnumbers("PXLX");
		//把数据传到页面
		request.setAttribute("fnumber", fnumber);
		request.setAttribute("type", type);
		return "/backHome/studentData/trainType/addAndUpdate";
	}
	
	/**
	 * 添加和修改
	 * @param request
	 * @param trainType 属性自动封装
	 * @return
	 */
	@RequestMapping("/addOrUpdateTyainType")
	public String addOrUpdateTyainType(HttpServletRequest request,TrainType trainType){
		//获取操作人信息
	    User user = (User)request.getSession().getAttribute("user");
	    //控制单元id
	    trainType.setFcontrolunitid(user.getFcompanyid());
	  	//获取当前时间
	  	String time = TimeFormat.timeFormat(new Date());
	  	if("".equals(trainType.getFid()) || null == trainType.getFid()){
			//创建人id
	  		trainType.setFcreatorid(user.getFid());
			//利用UUID生成fid
	  		trainType.setFid(GetFid.UUIDIntoFid());
			//当前创建时间
	  		trainType.setFcreatetime(time);
			//添加
	  		trainTypeService.addTrainType(trainType);
			//添加日志
			customLogService.addLog("学员基础资料", time+"对培训类型进行添加操作", null, user.getFid());
			//重定向到添加页面
			return "redirect:/DrivingSys/insertTrainType?type=3";
		}else{
			//修改时间
			trainType.setFlastupdatetime(time);
			//修改人id
			trainType.setFlastupdateuserid(user.getFid());
			//修改
			trainTypeService.modifyTrainType(trainType);
			//添加日志
			customLogService.addLog("学员基础资料", time+"对培训类型进行修改操作", null, user.getFid());
			 //调用查询的方法
            return commonController.skipUrl("/queryTrainType/look.action");
        }
	   
	}
	
	/**
	 * 根据fid查询
	 * @param request
	 * @param fid 要查询的fid
	 * @return
	 */
	@RequestMapping("/selectTrainTypeByFid")
	public String selectTrainTypeByFid(HttpServletRequest request,String fid){
		//查询数据
		TrainType trainType = trainTypeService.selTrainTypeById(fid);
		//把数据传到页面
		request.setAttribute("trainType", trainType);
		return "/backHome/studentData/trainType/addAndUpdate";
	}
	
	/**
	 * 根据fid删除
	 * @param request
	 * @param deleteId 要删除的fid，可以多个
	 * @return
	 */
	@RequestMapping("/deleteTrainTypeByFid")
	public String deleteTrainTypeByFid(HttpServletRequest request,String deleteId[],String fid){
		//获取操作人信息
	    User user = (User)request.getSession().getAttribute("user");
	    //删除
	    if(fid == null || "".equals(fid)){
	    	for (int i = 0; i < deleteId.length; i++) {
				trainTypeService.deleteTrainTypeByFid(deleteId[i]);
			}
	    }else{
	    	trainTypeService.deleteTrainTypeByFid(fid);
	    }
		
		//添加日志
		customLogService.addLog("学员基础资料", TimeFormat.timeFormat(new Date())+"对培训类型进行删除操作", null, user.getFid());
		//调用查询的方法
        queryTrainType(request, null);
        return "/backHome/studentData/trainType/trainTypeList";
	}



	/**
	 * 导出Execl
	 *
	 * @param response
	 */
	@RequestMapping("/importTrainType")
	public void importTrainType(HttpServletResponse response, HttpServletRequest request, String fnameL2) {
		//获取操作人信息
		User user = (User) request.getSession().getAttribute("user");
		List<Object> data = new ArrayList<>();
		//查询数据
        List<TrainType> trainTypes = trainTypeService.queryTrainTypeByCompanyIdAndFnameL2(user.getFcompanyid(), fnameL2.trim());
        for (TrainType trainType : trainTypes) {
			data.add(trainType.getFnumber());//编号
			data.add(trainType.getFnameL2());//名称
			data.add(trainType.getFsimplename());//班级简称
			data.add(trainType.getFdescriptionL2());//描述
			data.add(trainType.getFcreatetime());//创建时间
			data.add(trainType.getFlastupdatetime());//最后修改时间
			data.add(trainType.getFcreateName());//创建人
			data.add(trainType.getFlastupdateName());//修改人
		}

		//头部数据
		String head[] = {"编码", "名称", "简称", "描述", "创建时间", "最后修改时间","创建人", "修改人"};
		//长度
		Integer length[] = {5000, 7000, 5000, 5000, 6200, 6200, 3000, 3000};
		try {
			//导出Excel
			CommonExport.importExcel(response, "培训类型" + TimeFormat.getNowTime2() + ".xls", "培训类型", data, head, length);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
